<?php
namespace app\api\controller;

use app\api\annotation\LoginRequired;
use app\api\basic\BaseController;

use app\recruitment\logic\recruitmentLogic;
use app\recruitment\model\recruitment;
use app\recruitmentEnterprise\logic\recruitmentEnterpriseLogic;
use app\recruitmentEnterprise\model\recruitmentEnterprise;
use app\api\logic\CollectionLogic;
use support\Request;
class RecruitmentController extends BaseController
{
     protected $userInfo;

    /**
     * 当前登陆管理员ID
     */
    protected $userId;

    /**
     * 当前登陆管理员账号
     */
    protected $userName;
    public function __construct()
    {   
        parent::__construct();
        $this->userId=request()->userId;
        $this->userName=request()->userName;
        $this->userInfo=request()->userInfo;
    }
    /**
     * 招聘列表
     */
    #[LoginRequired(required: false)]
    public function lists(Request $request){

        $param = $request->more([
            ['lang_code','zh'],
            ['keyword',''],
            ['limit',10],
            ['page',1],
        ]);
        $recruitment=new recruitmentLogic();
        $query=$recruitment->search([
            'lang_code'=>$param['lang_code'],
        ]);
        if($param['keyword']){
            $query->where('position_name|position_description', 'like', "%{$param['keyword']}%");
        }
       // $where['lang_code']=$param['lang_code'];

        $recruitmentData=$query

            ->whereNull('delete_time')
            ->field('recruitment_id,position_name,salary_range,enterprise_id,work_experience,province_id,city_id,district_id,education_requirement,create_time')

            ->order('sort_order desc')
            ->order('create_time desc')
            ->paginate($param['limit']);
        return $this->success(['data'=>$recruitmentData->items(),'total'=>$recruitmentData->total()]);
    }

    /**
     * 招聘详情
     */
    #[LoginRequired(required: false)]
    public function detail(Request $request){
        $param = $request->more([
            ['recruitment_id',0],
        ]);
        $recruitment=new recruitmentLogic();
         $collectionLogic=new CollectionLogic();
        $recruitmentData=$recruitment
            ->where('recruitment_id',$param['recruitment_id'])
            ->field('recruitment_id,position_description,position_name,salary_range,enterprise_id,work_experience,province_id,city_id,district_id,education_requirement,create_time')
            ->whereNull('delete_time')
            ->find();
             $recruitmentData['company'] = recruitmentEnterprise::query()->where(['enterprise_id'=>$recruitmentData['enterprise_id']])->find();
          
             $recruitmentData['is_collection']=0;
            $collectionRes=$collectionLogic->where(['type'=>3,'collection_id'=>$param['recruitment_id'],'user_id'=>$this->userId])->findOrEmpty();
            if(!$collectionRes->isEmpty())$recruitmentData['is_collection']=1;
        return $this->success(['data'=>$recruitmentData]);
    }

    /**
     * 公司主页+职位列表
     */
    #[LoginRequired(required: false)]
    public function company(Request $request){
        $param = $request->more([
            ['enterprise_id',0],
        ]);
        $recruitmentEnterprise =new recruitmentEnterpriseLogic();
        $recruitmentEnterpriseData = $recruitmentEnterprise->where(['enterprise_id'=>$param['enterprise_id']])
            ->whereNull('delete_time')
            ->find();
        $recruitmentEnterpriseData['recruitment'] =recruitment::query()
            ->where(['enterprise_id'=>$recruitmentEnterpriseData['enterprise_id']])
            ->whereNull('delete_time')
            ->find();
        return $this->success(['data'=>$recruitmentEnterpriseData]);
    }




}